﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WebService.DataAccess;using WebService.BusinessEntities;
using WebService.DataAccess.Generic;
using System.Globalization;

namespace WebService.DataAccess
{
    /// <summary>
    /// This factory class takes a UserInvitation object and generates
    /// the dbcommand object needed to call the underlying update
    /// stored proc.
    /// </summary>
    internal class UserInvitationUpdateFactory : IDbToBusinessEntityNameMapper, IUpdateFactory<UserInvitation>
    {
        /// <summary>
        /// Creates the UserInvitationUpdateFactory to build an update statement for
        /// the given UserInvitation object.
        /// </summary>
        /// <param name="UserInvitation">UserInvitation to update into the database.</param>
        public UserInvitationUpdateFactory()
        {
        }

        #region IUpdateFactory<UserInvitation> Members

        public DbCommand ConstructUpdateCommand(Database db, UserInvitation userInvitation)
        {
            DbCommand command = db.GetStoredProcCommand("dbo.ACCEPT_USER_INVITATION");

            db.AddInParameter(command, "sent_by", DbType.Int32, userInvitation.sent_by);
            db.AddInParameter(command, "sent_to", DbType.Int32, userInvitation.sent_to);
            return command;
        }
        #endregion

        #region IDbToBusinessEntityNameMapper Members
        /// <summary>
        /// Maps a field name in the database (usually a parameter name for a stored proc)
        /// to the corresponding business entity property name.
        /// </summary>
        /// <remarks>This method is intended for error message handling, not for reflection.</remarks>
        /// <param name="dbParameter">Name of field/parameter that the database knows about.</param>
        /// <returns>Corresponding business entity field name.</returns>
        public string MapDbParameterToBusinessEntityProperty(string dbParameter)
        {
            switch (dbParameter)
            {
                case "sent_by":
                    return "sent_by";
                case "sent_to":
                    return "sent_to";
                default:
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter);
            }
        }
        #endregion
    }
}

